# vlc 文档 https://www.olivieraubert.net/vlc/python-ctypes/doc/

import sys
import time
import vlc
import keyboard

# 播放器对象
# 视频地址 窗口左上角X位置 窗口左上角Y位置
mediaUrlsAndPositions = [
    ['2.mp4', '--video-x=10', '--video-y=10'],
    ['1.mp4', '--video-x=190', '--video-y=190'],
]
# 播放器对象列表
media_objects = []


class MediaObject:
    def __init__(self, ins: vlc.Instance, player: vlc.MediaPlayer, media: vlc.Media):
        self.ins = ins
        self.player = player
        self.media = media


def PlayAll():
    for obj in media_objects:
        obj.player.play()
        obj.player.set_fullscreen(True)


def StopAll():
    for obj in media_objects:
        obj.player.stop()
        obj.player.release()
        obj.media.release()
        obj.ins.release()


# 定义按键处理函数p
def OnKeyPressed(event):
    if event.name.upper() == 'P':
        PlayAll()
        print("所有视频已开始播放。")
    elif event.name.upper() == 'S':
        StopAll()
        print("所有视频已停止。")
        sys.exit()  # 退出程序


if __name__ == "__main__":

    for urlAndPos in mediaUrlsAndPositions:
        _ins = vlc.Instance([
            '--video-on-top',  # 视频窗口总是在最前面
            '--no-video-title-show',  # 不显示视频标题
            urlAndPos[1],  # 视频窗口的X坐标
            urlAndPos[2],  # 视频窗口的Y坐标
            '--no-embedded-video'  # 不使用嵌入式视频
        ])
        _player = _ins.media_player_new()
        _media = _ins.media_new(urlAndPos[0])
        _player.set_media(_media)
        media_objects.append(MediaObject(_ins, _player, _media))

    # 注册按键监听事件
    keyboard.on_press(OnKeyPressed)

    # 保持程序不退出
    while True:
        time.sleep(1)
